package booroo.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JOptionPane;

import java.awt.CardLayout;
import java.awt.GridLayout;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTabbedPane;
import javax.swing.JComboBox;
import javax.swing.DropMode;
import javax.swing.JPasswordField;

public class AComp extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtUid;
	private JTextField textField;
	private final CardLayout cards = new CardLayout(0,0);
	private JPasswordField txtPwd;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			AComp dialog = new AComp();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public AComp() {
		setBounds(100, 100, 640, 480);
		setLocationRelativeTo(null);
		setTitle("고급 컴퍼넌트");
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.NORTH);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel top = new JPanel();
			top.setPreferredSize(new Dimension(10, 120));
			contentPanel.add(top, BorderLayout.NORTH);
			top.setLayout(new BorderLayout(0, 0));
			{
				JLabel topLogo = new JLabel("-");
				topLogo.setPreferredSize(new Dimension(400, 15));
				topLogo.setIcon(new ImageIcon(AComp.class.getResource("/booroo/swing/images/background/top.jpg")));
				top.add(topLogo, BorderLayout.WEST);
			}
			{
				final JPanel cardPane = new JPanel();
				cardPane.setPreferredSize(new Dimension(210, 10));
				top.add(cardPane, BorderLayout.EAST);
				cardPane.setLayout(cards);
				
				final JTabbedPane mainPane = new JTabbedPane(JTabbedPane.TOP);
				getContentPane().add(mainPane, BorderLayout.CENTER);
				
				JPanel basicFind = new JPanel();
				mainPane.addTab("기본검색", new ImageIcon(AComp.class.getResource("/booroo/swing/images/menu/exit.png")), basicFind, null);
				
				JButton btnNewButton = new JButton("New button");
				basicFind.add(btnNewButton);
				
				JPanel detailFind = new JPanel();
				mainPane.addTab("상세검색", new ImageIcon(AComp.class.getResource("/booroo/swing/images/menu/close.png")), detailFind, null);
				
				JPanel myPane = new JPanel();
				mainPane.addTab("마이페이지", new ImageIcon(AComp.class.getResource("/booroo/swing/images/menu/new.png")), myPane, null);
				mainPane.setEnabledAt(2, false);
				
				textField = new JTextField();
				detailFind.add(textField);
				textField.setColumns(10);
				
				
				
				JComboBox comboBox = new JComboBox();
				myPane.add(comboBox);
				
				
				
				JPanel loginpane = new JPanel();
				cardPane.add(loginpane, "login");
				
				JButton loginBtn = new JButton("로그인");
				loginBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
						if(txtUid.getText().equals("abc") && txtPwd.getText().equals("12345")){
							JOptionPane.showMessageDialog(null, "환영합니다!");
							mainPane.setEnabledAt(2,true);
							cards.show(cardPane, "logout");}
						else if(txtUid.getText().equals("") || txtPwd.getText().equals(""))
							JOptionPane.showMessageDialog(null, "아이디와 비밀번호를 입력하세요");
						else JOptionPane.showMessageDialog(null, "잘못 입력하셨습니다!");
												
					}
				});
				loginBtn.setMinimumSize(new Dimension(81, 23));
				
				JButton joinBtn = new JButton("회원가입");
				
				txtUid = new JTextField();
				txtUid.setColumns(10);
				
				txtPwd = new JPasswordField();
				txtPwd.setColumns(10);
				
				GroupLayout gl_loginpane = new GroupLayout(loginpane);
				gl_loginpane.setHorizontalGroup(
					gl_loginpane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_loginpane.createSequentialGroup()
							.addGroup(gl_loginpane.createParallelGroup(Alignment.LEADING, false)
								.addComponent(txtPwd)
								.addComponent(txtUid))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_loginpane.createParallelGroup(Alignment.LEADING)
								.addComponent(joinBtn, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(loginBtn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addContainerGap())
				);
				gl_loginpane.setVerticalGroup(
					gl_loginpane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_loginpane.createSequentialGroup()
							.addGap(33)
							.addGroup(gl_loginpane.createParallelGroup(Alignment.BASELINE)
								.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(loginBtn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_loginpane.createParallelGroup(Alignment.BASELINE)
								.addComponent(joinBtn)
								.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addContainerGap(35, Short.MAX_VALUE))
				);
				loginpane.setLayout(gl_loginpane);
				{
					JPanel logoutPane = new JPanel();
					cardPane.add(logoutPane, "logout");
					
					JLabel myAvatar = new JLabel("");
					myAvatar.setIcon(new ImageIcon(AComp.class.getResource("/booroo/swing/images/icon/icon2.png")));
					
					JButton logoutBtn = new JButton("로그아웃");
					logoutBtn.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							txtUid.setText("");
							txtPwd.setText("");
							JOptionPane.showMessageDialog(null, "잘가슈!");
							mainPane.setEnabledAt(2, false);
							cards.show(cardPane, "login");
						}
					});
					
					JLabel myNick = new JLabel("천송이");
					GroupLayout gl_logoutPane = new GroupLayout(logoutPane);
					gl_logoutPane.setHorizontalGroup(
						gl_logoutPane.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_logoutPane.createSequentialGroup()
								.addGap(21)
								.addComponent(myAvatar)
								.addGap(7)
								.addGroup(gl_logoutPane.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_logoutPane.createSequentialGroup()
										.addGap(49)
										.addComponent(myNick))
									.addGroup(gl_logoutPane.createSequentialGroup()
										.addGap(26)
										.addComponent(logoutBtn)))
								.addContainerGap())
					);
					gl_logoutPane.setVerticalGroup(
						gl_logoutPane.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_logoutPane.createSequentialGroup()
								.addGap(18)
								.addGroup(gl_logoutPane.createParallelGroup(Alignment.TRAILING)
									.addGroup(gl_logoutPane.createSequentialGroup()
										.addComponent(myNick)
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(logoutBtn))
									.addComponent(myAvatar))
								.addContainerGap(66, Short.MAX_VALUE))
					);
					logoutPane.setLayout(gl_logoutPane);
				}
			}
		}
		
		
	}
}
